/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.krazevina.luatgiaothong;

import com.sun.lwuit.Image;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author nna
 */
public class ImageDownload {
    public static Image getImageFromUrl(String url)
    {
        InputStream is = null;
        HttpConnection hc = null;
        Image img = null;
        try {
            hc = (HttpConnection)Connector.open(url);
            if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
                is = hc.openInputStream();
                int len = (int)hc.getLength();
                byte[] data = new byte[len];
                is.read(data);
                img = Image.createImage(data, 0, len);
            }
        } catch (Exception e) {
            System.out.println("IO Exception+"+e);
        } finally {
            if (is != null) {
                try {is.close();}
                catch (Exception e) { }
            }
            if (hc != null){
                try {hc.close();}
                catch (Exception e) { }
            }
            return img;
        }
    }
}
